package org.example.controller.impl;

import org.example.controller.RoleController;
import org.example.domain.Role;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roles")
public class RoleControllerImpl implements RoleController {
    @Autowired
    private RoleServiceImpl roleService;

    //获取所有角色信息
    @Override
    @GetMapping()
    @PreAuthorize("hasAuthority('role:select')")
    public ResponseResult getAllRoles() {
        return roleService.getAllRoles();
    }

    //修改用户角色
    @Override
    @PutMapping("/0/{userId}/{roleId}")
    @PreAuthorize("hasAuthority('role:update')")
    public ResponseResult updateUserRole(@PathVariable int userId, @PathVariable int roleId) {
        return roleService.updateUserRole(userId,roleId);
    }
    //修改角色信息及权限
    @Override
    @PutMapping("/1")
    @PreAuthorize("hasAuthority('role:update')")
    public ResponseResult updateRoleById(@RequestBody Role role) {
        return roleService.updateRoleById(role);
    }

    @Override
    @PostMapping()
    @PreAuthorize("hasAuthority('role:save')")
    public ResponseResult saveRole(@RequestBody Role role) {
        return roleService.saveRole(role);
    }

    @Override
    @DeleteMapping("/{roleId}")
    @PreAuthorize("hasAuthority('role:delete')")
    public ResponseResult deleteRole(@PathVariable int roleId) {
        return roleService.deleteRole(roleId);
    }

    @Override
    @GetMapping("/3/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('role:select')")
    public ResponseResult getRoleByPage(@PathVariable int pageSize,@PathVariable int currentPage) {
        return roleService.getRoleByPage(pageSize,currentPage);
    }

    @Override
    @GetMapping("/1/{pageSize}/{currentPage}/{roleKey}")
    @PreAuthorize("hasAuthority('role:select')")
    public ResponseResult getRoleByRoleKey(@PathVariable int pageSize,@PathVariable int currentPage,@PathVariable String roleKey) {
        return roleService.getRoleByRoleKey(pageSize,currentPage,roleKey);
    }
}
